import sql from '@/utils/sqlLite.js'
import {
	setObjSql,
	obj2str_sql
} from '@/utils/index.js'
// 数据表名称  价格表
export const deviceInfo = 'deviceInfo'
export const database = uni.getStorageSync('database')
/*

customerAccount 代理商账号
productId  产品设备id  
productModel 品类  
productProperty 设备属性 
stockStatus 租赁状态     1在库、2预定、4租赁中、6逾期、8丢失、10故障
stockReserveAccount  预定状态下，预定账号
battery 电池电量 
counter 计数器
productSecret 
*/
export const crateDeviceInfoTable = async () => {
	let sqltxt = [
		`create table if not exists ${deviceInfo}("customerAccount" VARCHAR(24),"productId" VARCHAR(24),"productModel" VARCHAR(24),"productProperty" VARCHAR(64),"battery" int(3),"stockStatus" int(2),"stockReserveAccount" VARCHAR(24),"counter" init(5),"productSecret" VARCHAR(24))`,
	];
	let res = await sql.executeSQL(uni.getStorageSync('database'), sqltxt);
	console.log(res);
}

// 添加设备
export const addDevice = async (pushData) => {

	// 数据库中productId 存在 修改 ，不存在 新增
	let sqltxt = `select *from ${deviceInfo} where productId = '${pushData.productId}'`;
	let res = await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
	console.log(res);
	if (res.code == 0 && res.data.length == 0) { //数据库中没有数据 新增

		let condition = Object.keys(pushData).join(',')
		let data = setObjSql(pushData)
		console.log(data);
		let sqltxt = `INSERT  INTO ${deviceInfo} (${condition}) VALUES(${data})`
		return await sql.executeSQL(uni.getStorageSync('database'), sqltxt);

	} else { //存在 ，更改
		let data = obj2str_sql(pushData);
		console.log(data);
		let sqltxt = `update  ${deviceInfo} set ${data} where productId = '${pushData.productId}'`;
		let rrs = await sql.executeSQL(uni.getStorageSync('database'), sqltxt);
		console.log(rrs);
	}
}

// 获取品类对应的设备详情
export const getDeviceTypeToId = async (productModel) => {
	let sqltxt = `select * from ${deviceInfo} where productModel = '${productModel}'`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}

// 获取品类对应的设备详情 productId
export const getDeviceTypeProductId = async (productId) => {
	let sqltxt = `select * from ${deviceInfo} where productId = '${productId}'`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}

export const getPidInStore = async (productModel) => {
	let sqltxt =
		`select * from ${deviceInfo} where productModel = '${productModel}' and (stockStatus = 1 or stockStatus = 2)`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}

// 设备计数器自动加一
export const addCounter = async (productId) => {
	let sqltxt = `update ${deviceInfo} set counter=counter+1 where productId = '${productId}'`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}


// 查询所有在库或者预定设备

export const getInStoreAll = async () => {
	let sqltxt = `select * from ${deviceInfo} where stockStatus = 1 or stockStatus = 2`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}

export const getNoLostList = async (productModel) => {
	let sqltxt =
		`select * from ${deviceInfo} where productModel = '${productModel}' and (stockStatus = 1 or stockStatus = 2 or stockStatus = 4 or stockStatus = 6 )`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}

// 获取所有的设备信息
export const getInStoreArr = async () => {
	let sqltxt = `select * from ${deviceInfo}`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}
// 清空表
export const delDeviceInfo = async () => {
	let sqltxt = `delete  from ${deviceInfo} `;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}

// 删除某一条
export const delPid = async (productId) => {
	let sqltxt = `delete  from ${deviceInfo} where productId = '${productId}' `;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}